package com.choco.jsondb.editor;

import com.choco.jsondb.Context;
import com.choco.jsondb.utils.FileUtil;
import com.choco.jsondb.utils.JSONUtil;

import java.util.HashMap;
import java.util.Map;

public class KVEditor implements Editor{
    private Map<String, String> jsonPairs;
    private Context context;

    public KVEditor(Context context){
        jsonPairs = new HashMap<>();
        this.context = context;
    }

    @Override
    public boolean put(String key, Object object){
        boolean result = false;

        if(object != null){
            jsonPairs.put(key, JSONUtil.getJsonString(object));
        }

        return result;
    }

    @Override
    public boolean put(String key, String jsonString) {
        boolean result = false;

        if(jsonString != null && jsonString.length() > 0) {
            jsonPairs.put(key, jsonString);
        }

        return result;
    }

    @Override
    public boolean put(Object object) {
        return false;
    }

    @Override
    public boolean put(String jsonString) {
        return false;
    }

    public boolean commit(){
        return FileUtil.write(context, jsonPairs);
    }
}
